/*
 * This class is the Hangman model and contains all information about the 
 * game as life left, what the right word are and send it to the Hangman View.
 */

package cth.tda367.beavers.glossaryGame.model.plugin;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;

import cth.tda367.beavers.glossaryGame.model.Letter;
import cth.tda367.beavers.glossaryGame.model.NormalScoreHandler;
import cth.tda367.beavers.glossaryGame.model.WordList;
import cth.tda367.beavers.glossaryGame.util.IObservable;



public class HangmanGame implements IObservable {
	
	WordList wordList;
	
	private int counter = 0;
	private int life;
	private String currentWord; 
	private char[] hangmanWord;
	private boolean findWord;
	private String hangmanWordString;
	ArrayList <Letter> chars = new ArrayList<Letter>();
	PropertyChangeSupport observers = new PropertyChangeSupport(this);
	NormalScoreHandler scoreHandl = new NormalScoreHandler();
	
	
	
	public HangmanGame (WordList wordList) {
		this.wordList = wordList;		
		newGameHangman();
		
	}
	public void newGameHangman(){
		
		life = 5;

		
		currentWord = wordList.getWord(counter).getLanguageSecond();
		
		hangmanWord = new char[currentWord.length()];
		
		scoreHandl.setListSize(wordList.getSize());
		scoreHandl.setPos(counter);
		scoreHandl.setScore(wordList.getWord(counter).getLanguageSecond().length());
		
		System.out.println(wordList.getWord(counter).getLanguageSecond().length());
		
		for(int i = 0; i < currentWord.length(); i++){
			hangmanWord[i] = '_';
		}	
	}
	
	public void revealHiddenWord() {
	}
	
	
	
	public void checkLetter(String stringLetter) {
		System.out.println("inside checkLetter");
		//Get the first char if StringLetter would contains more then one char. 
		char letter = stringLetter.toLowerCase().charAt(0);

		
		hangmanWordString = ""; 
		findWord = false;
		
		for(int i = 0; i < currentWord.length(); i++) {
			 
			if(currentWord.charAt(i) == letter){
				
				chars.add(new Letter(i,letter));
			
				hangmanWord[i] = currentWord.charAt(i);
				
				findWord = true;
								
			}	
			
		}
		// all chars with letters adds to hangmanWordString
		for(int p = 0; p < currentWord.length(); p++){
			
			hangmanWordString += (hangmanWord[p])+ " "; 
			
		}
		System.out.println(hangmanWordString);
		observers.firePropertyChange("test",null, hangmanWordString.toUpperCase());	
		
		checkWinning();
		checkGameOver();			
		
	}
	
	public void nextWord () {
		
		revealHiddenWord();	
		System.out.println("inside counter");
		try {
			
			counter++;
			scoreHandl.setPos(counter);
			scoreHandl.setScore(wordList.getWord(counter).getLanguageSecond().length());
			newGameHangman();
			checkLetter("_");
			
			
		}
		catch (IndexOutOfBoundsException e) {
			counter = wordList.getSize()-1;
			scoreHandl.setPos(counter);
		}
		
	}
	
	public NormalScoreHandler getHandler () {
		return scoreHandl;
	}

	public void checkGameOver() {
		if(!findWord){	
			life--;
			scoreHandl.remove(1);


		}
		if(life == 0){
			scoreHandl.setScore(0);
	
			try{
				firePropertyChangeListenerString("loosing");
				nextWord();
			}
			catch (IndexOutOfBoundsException e) {
				postResults();
				
			}
			
		}		
	}
	
	public void checkWinning(){
		boolean noblanket = true;
		for(int i = 0; i< currentWord.length(); i++){
			if(hangmanWord[i] == '_'){
				noblanket = false;
			}
		}
		if(noblanket){
			firePropertyChangeListenerString("winning");
		}
	}
	public char[] getHangmanWord(){
			return hangmanWord;
	}
	

	
	//only sends Strings. 
	public void firePropertyChangeListenerString(String text){
		observers.firePropertyChange(text,null, null);
	}
	
	
	public String getCurrentWord(){
		return currentWord;
	}
	
	
	public void postResults() {
	}
	

	@Override
	public void addObserver(PropertyChangeListener observer) {
		observers.addPropertyChangeListener(observer);
		
	}
	
	
}
